
var commuser=$api.getStorage('member2');

var imgItem=[];

apiready = function(){

     loadData();
	
};


function loadData(){

       
       $api.text($api.byId('realName'), commuser.realName);
       $api.text($api.byId('mobile'), commuser.mobile);
       $api.text($api.byId('fulladdress'), commuser.district+commuser.compounds+commuser.unitName +commuser.roomNumber);
       

};


//添加报修
function addrepair(){

    var explain=$api.byId('explain').value;
    if(explain.length<5){
        toast('描述太短啦!');
	    return;
    }

    var data={
        'linkman' :commuser.realName,
    	'mobile' : commuser.mobile,
    	'compounds' : commuser.compounds,
    	'address' :commuser.district+commuser.compounds+commuser.unitName +commuser.roomNumber,
    	'explain' :explain,
    	'type' :'公共报修',
    	'imgItem':imgItem,
    	
	};
	
	
	
	
	do_ajax("/app/repair/add2",'post',data,function(result,err){
		api.hideProgress();
		if(result.resultCode == '0') {
		    
		    toast('提交成功!');
		    setTimeout(function () {
 			    api.closeWin();
            }, 2000);
 			
 		}else{
 			 toast(result.datas);
 		}
 	});
	
    

};


//选择图片
function selimg(id){
    
    api.confirm({
	    title: "提示",
	    msg: "您想要从哪里选取图片 ?",
	    buttons:["优雅自拍", "相册收藏","取消"]
	},function(ret,err){
	
	    if(3 == ret.buttonIndex){ // 用户取消.
	        return;
	    }
	
	    var sourceType = "album";
	
	    if(1 == ret.buttonIndex){ /* 打开相机*/
	        sourceType = "camera";
	    }
	
	    api.getPicture({
	        sourceType: sourceType,
	        encodingType: "jpg",
	        mediaValue: "pic"
	    }, function(ret, err){
	        if (ret.data) {
	        	ajaxFileUpload(ret.data,id);
	        }
	    });
	});
    
    
};

/**
 * 上传文件 
 */
function ajaxFileUpload(data,id){


	var connectionType = api.connectionType;  //比如: wifi
	if(connectionType=='none'){toast('当前无网络!');return;}
	
	 api.showProgress({
        title: '正在上传...',
        text: '请稍等'
    });
    
	api.ajax({
        url:serviceUrl+'/community/upload/web',
        method: 'post',
        timeout: 30,
        dataType: 'json',
        returnAll: false,
        //report: true, //（可选项）是否实时返回上传文件进度
        data: {
            values: { name:"fileToUpload"},
            files: {fileToUpload: data }
        }
    },function(ret, err){
    		 if (ret) {
                if ($api.trim(ret.error) != '') {
                    toast(ret.error);
                }else {
                	//toast('上传成功!');
                	$api.attr($api.byId('img'+id),'src',serviceUrl+'/upload/web/'+ret.filename);
                	
                	imgItem.push({'url':'/upload/web/'+ret.filename});
                	
                	
                }
            } else {
                api.alert({
                    msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
                });
            }
            api.hideProgress();
    		
        });   
        
}













